<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="新闻标题" prop="newsTittle">
        <el-input
          v-model="queryParams.newsTittle"
          placeholder="请输入新闻标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻类型" prop="newsTypeId">
        <el-select v-model="queryParams.newsTypeId" placeholder="请选择服务类型">
          <el-option
              v-for="(i, k) in typeList"
              :key="k"
              :value="i.value"
              :label="i.label"
            />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:news:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:news:remove']"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="newsList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="新闻标题"
        align="center"
        prop="newsTittle"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="新闻类型"
        align="center"
        prop="newsTypeName"
        min-width="180"
      />
      <el-table-column label="发布时间" align="center" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.newsDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="publisher" min-width="180" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:news:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:news:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="form.newsId ? '修改新闻' : '新增新闻'"
      :visible.sync="open"
      width="50%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻标题" prop="newsTittle">
          <el-input v-model="form.newsTittle" placeholder="请输入新闻标题" />
        </el-form-item>
        <el-form-item label="新闻类型" prop="newsTypeId">
          <el-select
            v-model="form.newsTypeId"
            style="width: 100%"
            placeholder="请选择新闻类型"
          >
            <el-option
              v-for="(i, k) in typeList"
              :key="k"
              :value="i.value"
              :label="i.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="新闻内容" prop="newsContent">
          <!-- <Editors v-model="form.newsContent" :min-height="192" /> -->
          <Editors :content="form.newsContent" v-model="form.newsContent"  />
        </el-form-item>
        <el-form-item label="新闻封面" prop="coverUrl">
          <Image-upload v-model="form.coverUrl" :limit="1" :isShowTip="false" />
        </el-form-item>
        <el-form-item label="发布人" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入新闻标题" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNews, delNews, addNews, updateNews } from "@/api/system/news";

export default {
  name: "News",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      newsList: [], // 首页新闻表格数据
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        params: { pageNum: 1, pageSize: 10 },
        newsTypeId: null,
        newsTittle: null,
      },
      form: {}, // 表单参数
      rules: {
        // 表单校验
        newsTittle: [{ required: true, message: "新闻标题不能为空", trigger: "blur" }],
        newsContent: [{ required: true, message: "新闻内容不能为空", trigger: "blur" }],
        newsTypeId: [{ required: true, message: "新闻类型不能为空", trigger: "blur" }],
        coverUrl: [{ required: true, message: "新闻封面不能为空", trigger: "blur" }],
        publisher: [{ required: true, message: "发布人不能为空", trigger: "blur" }],
      },
      typeList: [
        { label: "平台公告", value: 1 },
        { label: "行业动态", value: 2 },
        { label: "政策法规", value: 3 },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询首页新闻列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams)
        .then((res) => {
          this.newsList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newsId: null,
        newsTypeId: null,
        newsTittle: null,
        newsContent: null,
        newsDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.newsId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.newsId) {
            updateNews(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.newsDate = new Date();
            addNews(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const newsIds = row.newsId ? [row.newsId] :this.ids;
      this.$modal
        .confirm('确认删除新闻编号为"' + newsIds + '"的数据项？')
        .then(() => {
          delNews(newsIds).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
